<?php

/**
 * If you are not authorized to modify this file, do NOT touch it.
 * @file app.class.php
 * @author deltamaster
 * @date 2010-12-2
 * @description The App object represents to the current running application. APP_PATH and APP_NAME should be defined before initialization.
 */

if (!defined('IN_IMBA')) exit('Access Denied.');

class App
{
	private static $appName;
	private static $appPath;
	private static $tpl = false;
    private static $tpl_style = false;
	private static $lang = array();

	public function __construct($appName, $appPath)
	{
		self::$appName = $appName;
		self::$appPath = $appPath;
	}
	
	/**
	 * @name SetTemplate
	 * @author deltamaster
	 * @date 2011-4-12
	 * @description
	 *   This is static member function which should be called like 'App::SetTemplate()'.
	 *   Set the template which should be applied to this application.
	 * @parameters
	 *   string $tpl: string of template name.
     *   style $style: string of template style.
	 * @return void
	 */
	public static function SetTemplate($tpl, $style = 'common')
	{
		if (is_string($tpl) || is_string($style))
        {
            self::$tpl = $tpl;
            self::$tpl_style = $style;
        }
	}
	
	/**
	 * @name LoadLanguageFile
	 * @author deltamaster
	 * @date 2011-3-22
	 * @description
	 *   This is static member function which should be called like 'App::LoadLanguageFile()'.
	 *   Load a new language file for the application. The language is specified by the configuration variable $imba_language.
	 * @parameters
	 * 	string $filename : should be a string. Determine the language file to be loaded.
	 * @return void
	 */
	public static function LoadLanguageFile($filename)
	{
		if (is_string($filename)) array_push(self::$lang, $filename);
	}

	/**
	 * @name Run
	 * @author deltamaster
	 * @date 2011-3-22
	 * @description
	 *   This is static member function which should be called like 'App::Run()'.
	 *   Once called, the control flow transfers to the business logic layer.
	 *   If an accessible template has been specified by SetTemplate method, it will automatically apply the template after running the business logic.
	 *   If any language files are loaded by LoadLanguageFile method, it will automatically load specified language files according to your language settings.
	 * @parameters void
	 * @return void
	 */
	public static function Run()
	{
		global $imba_language;
		global $db;
		require (self::$appPath . self::$appName . '.bll.php');
		/*
		$tempVars = get_defined_vars();
		foreach ($tempVars as $key => $value)
		{
			$GLOBALS[$key] = $value;
		}*/
		foreach (self::$lang as $l)
		{
			if (@is_readable(IMBA_LANGUAGE_DIR . $imba_language . '/' . $l . '.lang.php')) include(IMBA_LANGUAGE_DIR . $imba_language . '/' . $l . '.lang.php');
			else include(IMBA_LANGUAGE_DIR . 'common/' . $l . '.lang.php');
		}
		if (self::$tpl)
        {
            $tpl_ins = new Template(self::$tpl, self::$tpl_style);
            include($tpl_ins->GetPath());
        }
		if (OUTPUT_BUFFER) ob_end_flush();
	}
}

?>